﻿//
// Copyright (C) Ashish Kaila 2011
//

using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Input;
using System;

namespace Flipbook.Behaviors
{
    /// <summary>
    /// Maps manipulations to commands
    /// </summary>
    public class ManipulationToCommandBehavior : Behavior<FrameworkElement>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ManipulationToCommandBehavior"/> class.
        /// </summary>
        public ManipulationToCommandBehavior()
        {
            SupportedManipulations = ManipulationModes.All;    
        }

        /// <summary>
        /// Command property
        /// </summary>
        public static DependencyProperty CommandProperty = DependencyProperty.Register("Command",
                                                                                       typeof(ICommand),
                                                                                       typeof(ManipulationToCommandBehavior));

        /// <summary>
        /// ManipulationContainer property
        /// </summary>
        public static DependencyProperty ManipulationContainerProperty = DependencyProperty.Register("ManipulationContainer",
                                                                                                     typeof(IInputElement),
                                                                                                     typeof(ManipulationToCommandBehavior));

        /// <summary>
        /// Gets or sets the command.
        /// </summary>
        /// <value>
        /// The command.
        /// </value>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the manipulation container.
        /// </summary>
        /// <value>
        /// The manipulation container.
        /// </value>
        public IInputElement ManipulationContainer
        {
            get { return (IInputElement)GetValue(ManipulationContainerProperty); }
            set
            {
                SetValue(ManipulationContainerProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the supported manipulations.
        /// </summary>
        /// <value>
        /// The supported manipulations.
        /// </value>
        public ManipulationModes SupportedManipulations
        {
            get;
            set;
        }

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.IsManipulationEnabled = true;
            SetManipulationContainer();
            AssociatedObject.AddHandler(UIElement.ManipulationStartingEvent, new EventHandler<ManipulationStartingEventArgs>(OnManipulationStarting), false);
            AssociatedObject.AddHandler(UIElement.ManipulationDeltaEvent, new EventHandler<ManipulationDeltaEventArgs>(OnManipulationDelta), false);
        }

        /// <summary>
        /// Sets the manipulation container.
        /// </summary>
        private void SetManipulationContainer()
        {
            if (ManipulationContainer != null)
            {
                return;
            }

            DependencyObject parent = LogicalTreeHelper.GetParent(AssociatedObject);

            while ((parent != null) && ((parent as IInputElement) == null))
            {
                parent = LogicalTreeHelper.GetParent(AssociatedObject);
            }

            ManipulationContainer = parent as IInputElement;
        }

        /// <summary>
        /// Called when manipulation is starting
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ManipulationStartingEventArgs"/> instance containing the event data.</param>
        private void OnManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            if (ManipulationContainer == null)
            {
                return;
            }            
            
            e.ManipulationContainer = ManipulationContainer;
        }

        /// <summary>
        /// Called when manipulation delta occurs
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ManipulationDeltaEventArgs"/> instance containing the event data.</param>
        private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            if (Manipulation.IsManipulationActive(AssociatedObject))
            {                
                Manipulation.SetManipulationMode(AssociatedObject, Manipulation.GetManipulationMode(AssociatedObject) | SupportedManipulations);                
            }

            if ((Command != null) && (Command.CanExecute(e)))
            {
                Command.Execute(e);
                e.Handled = true;
            }
        }
    }
}
